package com.xh.bussiness.system.controller;

import com.xh.bussiness.system.bean.dto.BusinessFlowBean;
import com.xh.bussiness.system.bean.dto.BusinessNodeBean;
import com.xh.bussiness.system.bean.request.BusinessFlowRequest;
import com.xh.bussiness.system.service.IBusinessFlowService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;
import com.xh.core.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags="业务流程配置接口")
/*
 * 业务流程接口
 * @Author daiwei
 * @Description
 * @Date 2019/2/14 08:53
 **/
@RestController
@RequestMapping("/system/businessFlow")
public class BusinessFlowController extends BaseController {
    
    @Autowired
    private IBusinessFlowService businessFlowService;

    @ApiOperation(value="根据业务流程编码，获取业务流程数据", notes = "根据业务流程编码，获取业务流程数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="code", value="业务流程编码", required = true, dataType = "String"),
    })
    /**
     * 根据业务流程编码，获取业务流程数据
     * @param code
     * @return
     */
    @PostMapping("/queryFlows")
    public AjaxResult queryFlows(String code){

        BusinessNodeBean node1=new BusinessNodeBean();
        node1.setId("1");
        node1.setName("视频培训");
        node1.setUnitCode("prepareCourse_playVideo");

        BusinessNodeBean node2=new BusinessNodeBean();
        node2.setId("2");
        node2.setName("培训考试");
        node2.setUnitCode("prepareCourse_exam");

        BusinessNodeBean node3=new BusinessNodeBean();
        node3.setId("3");
        node3.setName("备课反馈");
        node3.setUnitCode("prepareCourse_feedback");

        BusinessNodeBean node4=new BusinessNodeBean();
        node4.setId("4");
        node4.setName("结束");
        node4.setUnitCode("end");

        List<BusinessNodeBean> result=new ArrayList<>();
        result.add(node1);
        result.add(node2);
        result.add(node3);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,null,result);
    }

    @ApiOperation(value="根据业务编码和父级id，查询下一节点数据", notes = "根据业务编码和父级id，查询下一节点数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="code", value="业务流程编码", required = true, dataType = "String"),
            @ApiImplicitParam(name="parentId", value="父节点ID，为空则返回第一个节点", required = false, dataType = "String"),
    })
    /**
     * 根据业务编码和父级id，查询下一节点数据
     * @param parentId
     * @return
     */
    @PostMapping("/getNextNode")
    public AjaxResult getNextNode(String code,String parentId){
        BusinessNodeBean node=null;
        if(StringUtils.isEmpty(parentId)){
            node=new BusinessNodeBean();
            node.setId("1");
            node.setName("视频培训");
            node.setUnitCode("prepareCourse_playVideo");
        }else if(parentId.equals("1")){
            node=new BusinessNodeBean();
            node.setId("2");
            node.setName("培训考试");
            node.setUnitCode("prepareCourse_exam");
        }else if(parentId.equals("2")){
            node=new BusinessNodeBean();
            node.setId("3");
            node.setName("备课反馈");
            node.setUnitCode("prepareCourse_feedback");
        }else if(parentId.equals("3")){
            node=new BusinessNodeBean();
            node.setId("4");
            node.setName("结束");
            node.setUnitCode("end");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,null,node);
    }

    /**
     * 分页查询
     */
    @ApiOperation(value = "业务流程配置分页查询", notes = "业务流程配置分页查询")
    @PostMapping("/queryPage")
    public XhPage queryPage(XhPage page, BusinessFlowRequest request) {
        return businessFlowService.queryBusinessFlowPage(page,request);
    }

    /**
     * 数据查询
     * @param request
     * @return
     */
    @ApiOperation(value="业务流程配置查询", notes = "业务流程配置查询")
    @PostMapping("/queryAll")
    public AjaxResult queryAll(BusinessFlowRequest request) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", businessFlowService.queryBusinessFlow(request));
    }


    /**
     * 新增和保存
     * @param businessFlowBean 实体对象
     * @return
     */
    @ApiOperation(value="数据字典数据保存", notes = "数据字典数据保存")
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated BusinessFlowBean businessFlowBean){
        if(businessFlowBean.getId()==null){
            businessFlowService.insertObj(businessFlowBean);
        }else{
            businessFlowService.insertObj(businessFlowBean);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"", businessFlowBean);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @ApiOperation(value="业务流程配置删除", notes = "业务流程配置删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value="业务流程配置id", required = true, dataType = "String"),
    })
    @PostMapping("/deleteById")
    @Transactional
    public AjaxResult deleteById(String id){
        businessFlowService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @ApiOperation(value="业务流程配置批量删除", notes = "业务流程配置批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ids", value="业务流程配置id集合", required = true, dataType = "String[]"),
    })
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        businessFlowService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }
}
